package com.yikes.srb.controller.web;

import com.yikes.srb.common.enums.ResultCodeEnum;
import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.userInfo.*;
import com.yikes.srb.leadingModle.vo.LoginVO;
import com.yikes.srb.leadingModle.vo.UserIndexVO;
import com.yikes.srb.leadingModle.vo.UserInfoVO;
import com.yikes.srb.pojo.enums.ServiceWorkCodeEnum;
import com.yikes.srb.service.UserInfoService;
import com.yikes.srb.web.utils.As;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "UserInfoController", description = "用户基本信息")
@RestController
@RequestMapping("/admin/user_info")
public class UserInfoController {

    private final UserInfoService service;

    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<UserInfoVO>> listPage(UserInfoQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<UserInfoVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody UserInfoAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody UserInfoEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "会员注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterForm form) {

        // 校验
        service.verify(form);
        // 注册
        service.register(form);

        return Result.ok();
    }

    @Operation(description = "会员登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginForm form, HttpServletRequest request) {

        As.isNotBlank(form.getMobile(), ServiceWorkCodeEnum.MOBILE_NULL_ERROR);
        As.isNotBlank(form.getPassword(), ServiceWorkCodeEnum.PASSWORD_NULL_ERROR);

        String ip = request.getRemoteAddr();
        LoginVO loginVO = service.login(form, ip);

        return Result.ok(loginVO);
    }

    @Operation(description = "校验令牌")
    @GetMapping("/checkToken")
    public Result checkToken(HttpServletRequest request) {

        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return Result.ok();
        } else {
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return Result.fail(ResultCodeEnum.LOGIN_AUTH_ERROR);
        }
    }

    @Operation(description = "锁定和解锁")
    @PutMapping("/lock/{id}/{status}")
    public Result lock(
            @Parameter(name = "用户id", required = true) @PathVariable("id") Long id,
            @Parameter(name = "锁定状态（0：锁定 1：解锁）", required = true) @PathVariable("status") Integer status) {

        service.lock(id, status);

        Result<Object> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(status == 1 ? "解锁成功" : "锁定成功");

        return result;
    }

    @Operation(description = "校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public Result checkMobile(@PathVariable String mobile) {

        boolean checkMobile = service.checkMobile(mobile);
        return Result.build(checkMobile);
    }

    @Operation(description = "获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public Result getIndexUserInfo(HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        UserIndexVO userIndexVO = service.getIndexUserInfo(userId);
        return Result.ok(userIndexVO);
    }

}
